package com.yunxi.service.localService.sys.impl;

import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.sys.RoleMenuMapper;
import com.yunxi.model.sys.Menu;
import com.yunxi.model.sys.RoleMenu;
import com.yunxi.service.localService.sys.RoleMenuService;
import com.yunxi.sys.pojo.IViewTree;
import com.yunxi.sys.pojo.RoleMenuVO;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by xuruzi on 2017/5/3.
 */
@Service
public class RoleMenuServiceImpl extends ServiceImplPlus<RoleMenuMapper,RoleMenu> implements RoleMenuService {

    @Override
    public List selectRoleMenuByRoleId(String roleId) {
        return baseMapper.listByRoleId(roleId);
    }

    public void saveUserMenuList(RoleMenu roleMenuExt, Long sysUserId) {
        //删除角色下的菜单
        baseMapper.deleteByRoleId(roleMenuExt.getRoleId());
        List<RoleMenu> ids = roleMenuExt.getRoleMenuList();
        //创建角色菜单
        if(ids!=null&&ids.size()>0){
            for(int i = 0 ; i < ids.size() ; i++){
                RoleMenu ext = ids.get(i);
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleMenuExt.getRoleId());
                roleMenu.setMenuId(ext.getMenuId());
                baseMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public Object findRoleMenuList(String roleId) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
        List list = baseMapper.queryByList(roleId);
        TreeConfig config = new TreeConfig("-1", "角色菜单", true, "menuId", "menuName", "pid", "hasChild","","hasChecked");
        return config.toTreeData(list, false);
    }

    @Override
    public List selectMenuIdByRoleId(Map map) {
        return null;
    }

    @Override
    public void saveRoleMenuList(RoleMenuVO roleMenuVO, String userId) {
        //删除角色下的菜单
        baseMapper.deleteByRoleId(roleMenuVO.getRoleId());
        List<IViewTree> ids = roleMenuVO.getRoleMenuList();
        //创建角色菜单
        if(ids!=null&&ids.size()>0){
            for(int i = 0 ; i < ids.size() ; i++){
                IViewTree ext = ids.get(i);
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleMenuVO.getRoleId());
                roleMenu.setMenuId(ext.getId());
                if(!ext.getId().equals("0")&&!ext.getId().equals("1")){
                    baseMapper.insert(roleMenu);
                }
            }
        }
    }

    @Override
    public List listByUserId(String userId) {
        return baseMapper.listByUserId(userId);
    }

    @Override
    public List listByRoleCode(String roleCode) {
        return baseMapper.listByRoleCode(roleCode);
    }
}
